<template>
    <view class="coupon">
        <MyNavbar :navList="navList" :currentIndex="currentIndex" @changeNav="changeNav"></MyNavbar>
        <view class="coupon_item" v-for="item in couponList" :key="item.id">
            <image class="bg" :class="getClass(item.status)" src="@/static/images/other/coupon_bg.png" />
            <view class="coupon_content">
                <view class="coupon_money">
                    <text class="unit">¥</text>
                    <text class="money">{{ item.discountPrice }}</text>
                </view>
                <view class="coupon_tips">
                    <view class="tips">{{ `满${item.usePrice}可用` }}</view>
                    <view class="use">有效期至: {{ getTime(item.validEndTime) }}</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import MyNavbar from '@/components/my-navbar/my-navbar.vue';
import { getCouponPage } from '@/api/other/coupon';
import { onLoad } from '@dcloudio/uni-app';
import { getTime } from '@/utils';

onLoad(() => {
    getCoupon()
})
// 导航
const navList = [
    { text: "未使用", index: 0, status: 1 },
    { text: "已使用", index: 1, status: 2 },
    { text: "已失效", index: 2, status: 3 }
]
const currentIndex = ref<number>(0)
const changeNav = ((e: number) => {
    if (e == currentIndex.value) return
    currentIndex.value = e
    getCoupon()
})
// 查询优惠券列表
const couponList = ref<any>([]);
const getCoupon = () => {
    const par = {
        status: navList[currentIndex.value].status
    }
    getCouponPage(par.status).then((res: any) => {
        if (res.code == 0 && res.data) {
            const list = res.data.list
            list.forEach((item: any) => {
                item.discountPrice = item.discountPrice / 100
                item.usePrice = item.usePrice / 100
            })
            couponList.value = list
        }
    })
}
const getClass = ((status:number) => {
    if (status === 2) {
        return 'use';
    } else if (status === 3) {
        return 'disabled';
    } else {
        return '';
    }
});

</script>

<style lang="scss" scoped>
.coupon {
    min-height: 100vh;
    overflow: hidden;
    background: #F8F8F8;
    padding-bottom: 200rpx;
    box-sizing: border-box;
}

.coupon_item {
    width: 698rpx;
    height: 152rpx;
    position: relative;
    margin: 0 auto;
    margin-top: 24rpx;
}

.bg {
    width: 698rpx;
    height: 152rpx;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}

.coupon_content {
    width: 698rpx;
    height: 152rpx;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    display: flex;
}

.coupon_money {
    width: 160rpx;
    height: 152rpx;
    line-height: 152rpx;
    font-family: 'DIN';
    font-weight: bold;
    color: #ffffff;
    display: flex;
    align-items: baseline;
    justify-content: center;

    .unit {
        font-size: 32rpx;
        margin-right: 8rpx;
    }

    .money {
        font-size: 64rpx;
    }
}

.coupon_tips {
    margin-left: 24rpx;

    .tips {
        font-weight: 700;
        font-size: 32rpx;
        color: #222222;
        margin-top: 24rpx;
    }

    .use {
        font-size: 26rpx;
        color: #888888;
        margin-top: 24rpx;
    }
}

.use {
    opacity: 0.6;
}

.disabled {
    opacity: 0.6;
    filter: grayscale(100%);
}
</style>